package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class Filter1_AdminLoginCheckFilter
 */
@WebFilter("/admin/*")
public class Filter1_AdminLoginCheckFilter implements Filter {

	public Filter1_AdminLoginCheckFilter() {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		String path = req.getServletPath();
		try {
			if (req.getSession().getAttribute("admin") == null && !path.substring(1, 4).equals("css")&& !path.substring(1, 3).equals("js") 
					&& !path.substring(1, 4).equals("img") && !path.substring(1, 8).equals("laydate") 
					&& !path.substring(1).equals("LoginServlet") && !path.substring(1).equals("login.html")
					&& !path.substring(1, 13).equals("LoginServlet")
					&& !path.substring(7, 10).equals("css")
					&& !path.substring(7, 9).equals("js") && !path.substring(7, 10).equals("img")
					&& !path.substring(7, 8).equals("laydate")
					&& !path.substring(7).equals("login.html")) {
				resp.sendRedirect(req.getContextPath() + "/admin/login.html");
			}else
				chain.doFilter(request, response);
		}catch(Exception e) {
			System.out.println(e.getMessage());
			resp.sendRedirect(req.getContextPath() + "/admin/login.html");
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
